package com.example.springbootfirst.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.example.springbootfirst.entity.UserEntity;
import com.example.springbootfirst.exception.BaseException;
import com.example.springbootfirst.repository.UserRepository;
import com.example.springbootfirst.service.UserService;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * created by Morgan on 2021/10/2
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public String login(UserEntity entity) {
        if (entity == null) {
            throw new BaseException("998","登录异常");
        }
        List<UserEntity> list = userRepository.findAll(Example.of(entity));
        if (CollUtil.isEmpty(list)) {
            throw new BaseException("998","登录失败，用户名或密码错误");
        }else if (list.size() > 1){
            throw new BaseException("998","登录异常");
        }
        return IdUtil.fastSimpleUUID();
    }
}
